<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\services\car;


use app\dao\car\BaseSendFeeDao;
use app\services\BaseServices;
use crmeb\exceptions\AdminException;
use crmeb\services\FormBuilder as Form;
use think\facade\Route as Url;

/**
 * Class BaseSendFeeServices
 * @package app\services\shendeng
 */
class BaseSendFeeServices extends BaseServices
{
    public function __construct(BaseSendFeeDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 列表
     * @param array $where
     * @param bool $is_page
     * @return array
     */
    public function getList(array $where = [],bool $is_page = false){
        $page = $limit = 0;
        if($is_page){
            [$page, $limit] = $this->getPageValue();
        }
        $list = $this->dao->getList($where,$page,$limit);
        foreach ($list['list'] as &$item){
            $item['start_time'] = $item['start_time'] ? date('H:i',$item['start_time']) : '未设置';
            $item['end_time'] = $item['end_time'] ? date('H:i',$item['end_time']) : '未设置';
        }
        return $list;
    }

    /**
     * 添加/修改
     * @param int $id
     * @param array $admin
     * @return array
     * @throws \FormBuilder\Exception\FormBuilderException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(int $id,array $admin){
        $sendFee = $this->dao->get($id);
        if($sendFee){
            $sendFee['start_time'] = date('H:s',$sendFee['start_time']);
            $sendFee['end_time'] = date('H:s',$sendFee['end_time']);
        }
        $placeServices = app()->make(BasePlaceServices::class);
        $place = $placeServices->getStoreList();
//        $date = [
//            ['value'=>30,'label'=>'30分钟预约'],
//            ['value'=>60,'label'=>'60分钟预约'],
//            ['value'=>90,'label'=>'90分钟预约'],
//            ['value'=>120,'label'=>'120分钟预约'],
//        ];
        $field = array();
        $title = $id ? '修改' : '添加';
        $field[] = Form::hidden('id', $id);
        if($admin['store_id']){
            $field[] = Form::select('store_id', '所属门店', $admin['store_id'])->setOptions($place)->filterable(true)->filterable(true)->disabled(true);
        }else{
            $field[] = Form::select('store_id', '所属门店', $sendFee['store_id']??'')->setOptions($place)->filterable(true)->filterable(true);
        }
        $field[] = Form::time('start_time', '服务开始时间', $sendFee['start_time']??'')->format('HH:mm')->style('width:100%');
        $field[] = Form::time('end_time', '服务结束时间', $sendFee['end_time']??'')->format('HH:mm')->style('width:100%');
        $field[] = Form::input('mink_km', '最小公里数', $sendFee['mink_km']??'')->placeholder('单位/km');
        $field[] = Form::input('max_km', '最大公里数', $sendFee['max_km']??'')->placeholder('单位/km');
        $field[] = Form::input('send_fee', '服务费用', $sendFee['send_fee']??'')->placeholder('请输入');
        $field[] = Form::input('before_min', '预约时间', $sendFee['before_min']??'')->placeholder('提前多少分钟预约');
        return create_form($title, $field, Url::buildUrl('/base/sendFee/save'), 'POST');
    }

    /**
     * 添加/修改保存
     * @param int $id
     * @param array $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(int $id, array $data){
        $sendFee = $this->dao->getOne(['store_id' => $data['store_id'],'is_del'=>0]);
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        if($data['start_time'] > $data['end_time']){
            throw new AdminException('结束时间不能小于开始时间');
        }
        if ($id) {
            if (!$this->dao->get($id)) throw new AdminException('数据不存在');
            if ($sendFee && $id != $sendFee['id']) throw new AdminException('该门店已设置');
            if ($this->dao->update($id, $data)) {
                return true;
            } else {
                throw new AdminException('修改失败或者您没有修改什么');
            }
        } else {
            unset($data['id']);
            if ($sendFee) throw new AdminException('该门店已设置');
            $data['add_time'] = time();
            if ($this->dao->save($data)) {
                return true;
            } else {
                throw new AdminException('添加失败');
            }
        }
    }

    /**
     * 删除
     * @param int $id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delSendFee(int $id){
        $city = $this->dao->get($id);
        if ($city) {
            $data['is_del'] = 1;
            $data['delete_time'] = time();
            if (!$this->dao->update($id,$data)) {
                throw new AdminException('删除失败,请稍候再试!');
            }
        }
        return '删除成功!';
    }
}
